#!/usr/bin/env python3
"""
颜色选择器修复验证脚本
验证所有已知问题都已修复
"""

import sys
from PySide6.QtWidgets import QApplication
from PySide6.QtGui import QColor

def verify_imports():
    """验证导入修复"""
    print("🔍 验证导入修复")
    print("-" * 30)
    
    try:
        from dialogs.color_picker import ColorPickerDialog
        print("✅ ColorPickerDialog 导入成功")
        
        # 检查QCheckBox是否可用
        from PySide6.QtWidgets import QCheckBox
        print("✅ QCheckBox 导入成功")
        
        return True
    except ImportError as e:
        print(f"❌ 导入失败: {e}")
        return False

def verify_initialization():
    """验证属性初始化修复"""
    print("\n🔧 验证属性初始化修复")
    print("-" * 30)
    
    try:
        app = QApplication(sys.argv)
        from dialogs.color_picker import ColorPickerDialog
        
        # 创建对话框
        dialog = ColorPickerDialog()
        
        # 检查属性是否正确初始化
        required_attrs = [
            'detection_area_enabled',
            'detection_method',
            'dominant_region_size',
            'dominant_center_offset_x',
            'dominant_center_offset_y',
            'kmeans_clusters'
        ]
        
        for attr in required_attrs:
            if hasattr(dialog, attr):
                value = getattr(dialog, attr)
                print(f"✅ {attr}: {value}")
            else:
                print(f"❌ {attr}: 未找到")
                return False
        
        return True
        
    except Exception as e:
        print(f"❌ 初始化验证失败: {e}")
        return False

def verify_ui_components():
    """验证UI组件创建"""
    print("\n🎨 验证UI组件创建")
    print("-" * 30)
    
    try:
        app = QApplication(sys.argv)
        from dialogs.color_picker import ColorPickerDialog
        
        dialog = ColorPickerDialog()
        
        # 检查UI组件是否正确创建
        ui_components = [
            'enable_detection_area_cb',
            'detection_method_combo',
            'region_size_spin',
            'offset_x_spin',
            'offset_y_spin',
            'clusters_spin',
            'detection_area_info_label'
        ]
        
        for component in ui_components:
            if hasattr(dialog, component):
                print(f"✅ {component}: 已创建")
            else:
                print(f"❌ {component}: 未找到")
                return False
        
        return True
        
    except Exception as e:
        print(f"❌ UI组件验证失败: {e}")
        import traceback
        traceback.print_exc()
        return False

def verify_safety_checks():
    """验证安全检查"""
    print("\n🛡️ 验证安全检查")
    print("-" * 30)
    
    try:
        app = QApplication(sys.argv)
        from dialogs.color_picker import ColorPickerDialog
        
        dialog = ColorPickerDialog()
        
        # 测试安全检查方法
        print("测试 update_detection_area_info:")
        dialog.update_detection_area_info()
        print("✅ update_detection_area_info 安全检查通过")
        
        print("测试 on_detection_area_enabled_changed:")
        dialog.on_detection_area_enabled_changed(True)
        dialog.on_detection_area_enabled_changed(False)
        print("✅ on_detection_area_enabled_changed 安全检查通过")
        
        print("测试 on_detection_method_changed:")
        dialog.on_detection_method_changed("dominant_kmeans")
        dialog.on_detection_method_changed("traditional")
        print("✅ on_detection_method_changed 安全检查通过")
        
        return True
        
    except Exception as e:
        print(f"❌ 安全检查验证失败: {e}")
        import traceback
        traceback.print_exc()
        return False

def verify_functionality():
    """验证功能完整性"""
    print("\n⚙️ 验证功能完整性")
    print("-" * 30)
    
    try:
        app = QApplication(sys.argv)
        from dialogs.color_picker import ColorPickerDialog
        
        dialog = ColorPickerDialog()
        
        # 测试启用/禁用功能
        print("测试启用/禁用功能:")
        dialog.enable_detection_area_cb.setChecked(True)
        print(f"  启用状态: {dialog.detection_area_enabled}")
        
        dialog.enable_detection_area_cb.setChecked(False)
        print(f"  禁用状态: {dialog.detection_area_enabled}")
        
        # 测试预设功能
        print("测试预设功能:")
        presets = ["fps", "moba", "rpg"]
        for preset in presets:
            dialog.apply_detection_preset(preset)
            print(f"  {preset.upper()}预设: 区域大小={dialog.region_size_spin.value()}")
        
        # 测试数据获取
        print("测试数据获取:")
        dialog.detection_area_enabled = True
        color_data = dialog.get_color_data()
        
        if "detection_area_enabled" in color_data:
            print(f"  检测区域启用: {color_data['detection_area_enabled']}")
            if color_data['detection_area_enabled']:
                print(f"  检测方法: {color_data.get('detection_method', 'N/A')}")
                print(f"  区域大小: {color_data.get('dominant_region_size', 'N/A')}")
        
        return True
        
    except Exception as e:
        print(f"❌ 功能验证失败: {e}")
        import traceback
        traceback.print_exc()
        return False

def main():
    """主验证函数"""
    print("🚀 颜色选择器修复验证")
    print("验证所有已知问题都已修复")
    print("=" * 50)
    
    tests = [
        ("导入修复", verify_imports),
        ("属性初始化", verify_initialization),
        ("UI组件创建", verify_ui_components),
        ("安全检查", verify_safety_checks),
        ("功能完整性", verify_functionality)
    ]
    
    passed = 0
    total = len(tests)
    
    for test_name, test_func in tests:
        print(f"\n📋 {test_name}")
        if test_func():
            passed += 1
            print(f"✅ {test_name} 验证通过")
        else:
            print(f"❌ {test_name} 验证失败")
    
    print(f"\n" + "=" * 50)
    print(f"验证结果: {passed}/{total} 通过")
    
    if passed == total:
        print("🎉 所有修复验证通过！")
        print("\n✅ 修复内容:")
        print("1. ✅ QCheckBox 导入错误已修复")
        print("2. ✅ 属性初始化顺序已修复")
        print("3. ✅ 安全检查已增强")
        print("4. ✅ 预览功能已优化")
        print("5. ✅ UI组件创建正常")
        print("6. ✅ 功能完整性验证通过")
        
        print("\n💡 现在可以正常使用:")
        print("- 点击'添加颜色'按钮")
        print("- 展开'检测区域设置'")
        print("- 配置检测参数")
        print("- 使用游戏预设")
        print("- 预览检测区域")
        
        return True
    else:
        print("❌ 部分验证失败，请检查错误信息")
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)
